import xml.utils.XmlDocument;
import xml.utils.XmlException;
import xml.utils.XmlNode;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.sql.rowset.WebRowSet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.sun.rowset.WebRowSetImpl;


public class AddUserDialog extends JDialog {
	private JTextField txtUserID;
	private JTextField txtNameofUser;
	private JTextField txtPassword;
	private JButton btnCancel;
	private JComboBox cmbUserType;
	private JButton btnOk;
	private JLabel lblUserType;
	private JLabel lblUserid;
	private JLabel lblAddNewUser;
	private JLabel lblName;
	private JLabel lblPassword;

	String strUserID;
	String strNameofUser;
	int iUserType;
	String strUserTypeID;
	String strPassword;
	int iWarehouse;
	rsTableModel rs;
	AccessDBInterface accClientThis;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			AddUserDialog dialog = new AddUserDialog(null, 0);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public AddUserDialog(AccessDBInterface accClient, int numberofservers) {
		
		this.accClientThis = accClient;
		
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(null);
		
		lblAddNewUser = new JLabel("ADD NEW USER");
		lblAddNewUser.setFont(new Font("Tahoma", Font.BOLD, 20));
		lblAddNewUser.setHorizontalAlignment(SwingConstants.CENTER);
		lblAddNewUser.setBounds(133, 11, 160, 14);
		getContentPane().add(lblAddNewUser);
		
		lblUserid = new JLabel("UserID");
		lblUserid.setBounds(42, 47, 46, 14);
		getContentPane().add(lblUserid);
		
		txtUserID = new JTextField();
		txtUserID.setBounds(113, 44, 237, 20);
		getContentPane().add(txtUserID);
		txtUserID.setColumns(10);
		
		lblName = new JLabel("Name");
		lblName.setBounds(42, 80, 46, 14);
		getContentPane().add(lblName);
		
		txtNameofUser = new JTextField();
		txtNameofUser.setBounds(113, 77, 237, 20);
		getContentPane().add(txtNameofUser);
		txtNameofUser.setColumns(10);
		
		lblUserType = new JLabel("User Type");
		lblUserType.setBounds(42, 115, 59, 14);
		getContentPane().add(lblUserType);

		rs = null;
		try {
			rs = LoadUserType(accClient);
		} catch (RemoteException | SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Vector<String> vecUserType = new Vector<>(rs.getRowCount());
		for (int i = 0; i < rs.getRowCount(); i++) {
			vecUserType.add("" + rs.getValueAt(i, 1));
		}
		cmbUserType = new JComboBox(vecUserType);
		cmbUserType.setBounds(112, 112, 237, 20);
		getContentPane().add(cmbUserType);
		
		lblPassword = new JLabel("Password");
		lblPassword.setBounds(42, 151, 46, 14);
		getContentPane().add(lblPassword);
		
		txtPassword = new JTextField();
		txtPassword.setBounds(113, 148, 237, 20);
		getContentPane().add(txtPassword);
		txtPassword.setColumns(10);
		
		btnOk = new JButton("OK");
		btnOk.setBounds(82, 228, 89, 23);
		getContentPane().add(btnOk);
		
		btnCancel = new JButton("Cancel");
		btnCancel.setBounds(249, 228, 89, 23);
		getContentPane().add(btnCancel);
		
		Vector<String> vecWarehouse = new Vector<>(numberofservers);
		for (int i = 0; i < numberofservers; i++) {
			vecWarehouse.add("Server " + (i + 1));
		}
		
		btnOk.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				strUserID = txtUserID.getText();
				strNameofUser = txtNameofUser.getText();
				iUserType = cmbUserType.getSelectedIndex();
				strUserTypeID = rs.getValueAt(iUserType, 0).toString();
				strPassword = txtPassword.getText();
				
				if (strUserID.equals("") || strNameofUser.equals("") || strPassword.equals(""))
					JOptionPane.showMessageDialog(null, "You cannot leave any blank");
				else {
					String strQuery = "insert into User values ('" + strUserID + "', '" + 
																strNameofUser + "', '" + 
																strUserTypeID + "', '" + 
																strPassword + "')";
					try {
						int i = accClientThis.ExecUpdateSQL(strQuery);
						if (i != 0)
							setVisible(false);
					} catch (RemoteException e1) {
						// TODO Auto-generated catch block
						JOptionPane.showMessageDialog(null, e1.toString());
					}				
				}
			}
		});
		
		btnCancel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				setVisible(false);
			}
		});
	}
	
	public rsTableModel LoadUserType(AccessDBInterface accClient) throws RemoteException, SQLException {
		ResultSet r = accClient.ExecQuerySQL("select UserTypeID, NameofType from UserType");
		WebRowSet wrs = new WebRowSetImpl();
		wrs.populate(r);
		rsTableModel rs = new rsTableModel(wrs);
		
		return rs;
	}
	
	public JButton getBtnCancel() {
		return btnCancel;
	}
	public JComboBox getComboBox() {
		return cmbUserType;
	}
	public JTextField getTxtNameofUser() {
		return txtNameofUser;
	}
	public JTextField getTxtUserID() {
		return txtUserID;
	}
	public JButton getBtnOk() {
		return btnOk;
	}
	public JTextField getTxtPassword() {
		return txtPassword;
	}
	public JLabel getLblUserType() {
		return lblUserType;
	}
	public JLabel getLblUserid() {
		return lblUserid;
	}
	public JLabel getLblAddNewUser() {
		return lblAddNewUser;
	}
	public JLabel getLblName() {
		return lblName;
	}
	public JLabel getLblPassword() {
		return lblPassword;
	}
}
